#include<stdio.h>
#include<assert.h>

void InsertSort(int* a, int n)
{
	assert(a);

	for (int i = 0; i < n-1; i++)
	{
		int end = i;
		int tem = a[end + 1];

		while (end >= 0)
		{
			if (a[end] > tem)
			{
				a[end + 1] = a[end];
				end--;
			}
			else
			{
				break;

			}
		}
		a[end + 1] = tem;
	}

}
void ShellSort1(int* a, int n)
{
	int gap =n;
	
	while (gap > 1)
	{
		gap /= 2;
		for (int j = 0; j < gap; j++)
		{
			for (int i = j; i < n - gap; i += gap)
			{
				int end = i;
				while (end >= 0)
				{
					int tem = a[end + gap];
					if (a[end] > a[end + gap])
					{
						a[end + gap] = a[end];
						end -= gap;

					}
					else
					{
						break;
					}
					a[end + gap] = tem;

				}
			}

		}

	}
}

void ShellSort2(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap /= 2;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tem = a[end + gap];

			while (end >= 0)
			{
				if (a[end + gap] < a[end])
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tem;

		}
	}
}



void Printarr(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);

	}
	printf("\n");
}

void testInsert()
{
	int arr[] = { 4,6 ,2,3,7 ,9,1,5,10};
	int n = sizeof(arr)/sizeof(int);
	InsertSort(arr, n);
	Printarr(arr, n);
}
void testShell()
{
	int arr[] = { 4,6 ,2,3,7 ,9,1,5,10 };
	int n = sizeof(arr) / sizeof(int);

	ShellSort1(arr, n);

	Printarr(arr, n);

	ShellSort2(arr, n);

	Printarr(arr, n);
}
int main()
{

	testShell();
	return 0;
}